package com.lys.emsystem.service.impl;

import com.lys.emsystem.entity.*;
import com.lys.emsystem.mapper.*;
import com.lys.emsystem.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lys.emsystem.service.SchoolService;
import com.lys.emsystem.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liyansheng
 * @since 2022-05-02
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Autowired
    private SchoolService schoolService;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private LoginLogMapper loginLogMapper;

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private TripartiteMapper tripartiteMapper;

    @Autowired
    private IntentMapper intentMapper;

    @Override
    public Result getAnalyzeData(HttpSession session) {
        HashMap<String, Object> map = new HashMap<>();
        //查询学校数量
        List<School> schools = schoolService.selectList();
        map.put("schoolCount", schools.size());
        //用户数量
        List<Admin> admins = adminMapper.selectList(null);
        List<Student> students = studentMapper.selectList(null);
        List<Teacher> teachers = teacherMapper.selectList(null);
        map.put("userCount", students.size() + teachers.size());
        //系統访问量
        List<LoginLog> loginLogs = loginLogMapper.selectList(null);
        map.put("visitCount", loginLogs.size());
        // 招聘信息量
        List<Job> jobs = jobMapper.selectList(null);
        map.put("jobCount", jobs.size());
        // 管理员数量
        map.put("adminCount", admins.size());
        //在线用户量
        map.put("onlineCount", session.getAttribute("onlineCount"));
        //系统访问趋势
        List<ChartData> visitTrend = loginLogMapper.getVisitTrend();
        map.put("visitTrend", visitTrend);
        //用户性别
        int man = 0;
        int woman = 0;
        for (Student student : students) {
            if (student.getSex() == 1) {
                man = man + 1;
            }
            if (student.getSex() == 0) {
                woman = woman + 1;
            }
        }
        for (Teacher teacher : teachers) {
            if (teacher.getSex()) {
                man = man + 1;
            }
            if (!teacher.getSex()) {
                woman = woman + 1;
            }
        }
        List<ChartData> sexData = new ArrayList<>();
        sexData.add(new ChartData("男", man));
        sexData.add(new ChartData("女", woman));
        map.put("sexData", sexData);
        //已签约薪资分布
        List<ChartData> salary = tripartiteMapper.getSalary();
        map.put("salary", salary);
        //意向工作城市
        List<ChartData> intentCityData = intentMapper.getIntentCityData(null);
        for (ChartData chartData : intentCityData) {
            String name=chartData.getName();
            if (name.equals("内蒙古自治区")||name.equals("黑龙江省")) {
                chartData.setName(name.substring(0, 3));
            } else {
                chartData.setName(name.substring(0, 2));
            }
        }
        map.put("cityData", intentCityData);
        return Result.buildResult(Result.Status.OK, map);
    }
}
